<?php
/**
 * Copyright (c) 2010, Slawomir Mazur <mazursllawek@gmail.com>
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions 
 * are met:
 * 
 * o Redistributions of source code must retain the above copyright notice, 
 *   this list of conditions and the following disclaimer.
 * o Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * o Neither the name of the Softisland nor the names of its contributors
 *   may be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/**
 * Enter description here...
 *
 * @package     File
 * @copyright   Slawomir Mazur (c) 2010 (http://www.zend.com/zce.php?c=ZEND011643)
 * @license     http://www.opensource.org/licenses/bsd-license.php
 * @author      Slawomir Mazur <mazursllawek@gmail.com>
 */

abstract class Gp_File_Abstract
{
	/**
	 * File id (database)
	 * 
	 * @var int
	 */
	protected $_fileInstance;

	protected $_reader;
	
	protected $_fileName;
	
	protected $_geneIdColumnIndex;
	
	protected $_columnsMap;
	
	// filters settings
	protected $_loadWhole = false;
	
	protected $_startRow = false;
	
	protected $_endRow = false;
	
	protected $_readRows = false;
	
	protected $_omitRows = false;
	
	protected $_columns = false;
	
	
	const BASIC_EXCEL_UPLOAD_PATH = 'upload';
	
	public function __construct($id)
	{
		if (empty($id)) {
			throw new Exception('No file id selected');
		}
		$this->_loadFile($id);
	}
	
	/**
	 * 
	 * Parsing function for reading files
	 */
    abstract public function parse();
    
	public function setLoadWhole($flag = true)
	{
		$this->_loadWhole = $flag;
	}
	
	public function setStartRow($row = null)
	{
		$this->_startRow = (int) $row;
		// extra plus because of first heading row
		$this->_startRow++;
	}
	
	public function setEndRow($row = null)
	{
		$this->_endRow = (int) $row;
		// extra plus because of first heading row
		$this->_endRow++;
	}
	
	public function setRowsToRead($rows = null)
	{
		if (!empty($rows) && (false !== strpos($rows, ','))) {
			$this->_readRows = explode(',', $rows);
			// extra plus because of first heading row
			foreach ($this->_readRows as &$row) {
				$row++;
			}
		}
	}
	
	public function setRowsToOmit($rows = null)
	{
		if (!empty($rows) && (false !== strpos($rows, ','))) {
			$this->_omitRows = explode(',', $rows);
			// extra plus because of first heading row
			foreach ($this->_omitRows as &$row) {
				$row++;
			}
			// for O(1) access
			$this->_omitRows = array_flip($this->_omitRows);
		}
	}
	
	public function setColumnsToRead($columns = null)
	{
		if (!empty($columns) && is_array($columns)) {
			$this->_columns = $columns;
		}
	}
	
	public function setGeneIdColumn($id)
	{
		$this->_geneIdColumnIndex = (int) $id;
	}
	
	public function setColumnsMapper(array $map = array())
	{
		$this->_columnsMap = $map;
	}
    
	/**
	 * Get loaded file object
	 * 
	 * @return Zend_Db_Table_Row
	 */
	public function getFileInstance()
	{
		return $this->_fileInstance;
	}
	
	protected function _loadFile($id)
	{
		$fileModel = Gp_App::getModel('system/library')->getLibraryById($id);
		
		if ($fileModel->library_id != $id) {
			throw new Exception('Id\'s doesn\'t match');
		}
		
		$this->_fileInstance = $fileModel;
		return $this;
	}
}